/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 */
package org.javaongems.client.winctrls;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.javaongems.client.stdctrls.MenuBar;
import org.javaongems.client.stdctrls.MenuItem;


import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;

/**
 * <p>Provides a breadcrumb widget that is modeled on the vist breadcrumb bar.</p>
 * <h3>CSS Style Rules</h3>
 * <ul class='css'>
 * <li>.BreadcrumbBar { the menu bar itself }</li>
 * <li>.gems-BreadcrumbBar .gems-Crumb { menu items }</li>
 * <li>.gems-BreadcrumbBar .gems-Crumb selected { selected menu items }</li>
 * </ul>
 * 
 * @author Ashin Wimalajeew (ash)
 */
public class BreadcrumbBar extends Composite {
	static public interface Listener {
		public void onLoadSignPostCrumbs(Crumb crumb);
		public void onSelect(Crumb crumb);
	}

	private boolean isInBrowserEvent;
	private boolean doneDynamicRefresh;
	private Bar bar = new Bar(false);
	private HashMap signPostMap = new HashMap();
	private boolean addSignPosts;
	private Listener listener;

	public BreadcrumbBar() {
		initWidget(bar);
	}

	public void setAddSignPosts(boolean useSignPosts) {
		addSignPosts = useSignPosts;
	}
	
	public void setListener(Listener lsnr) {
		listener = lsnr;
	}

	public void addCrumb(String label, String ref) {
		Crumb toAdd = new Crumb(ref, label, true);
		bar.addItem(toAdd);
		if (addSignPosts) {
			Crumb signPost = new Crumb(ref);
			bar.addItem(signPost);
			signPostMap.put(ref, signPost);
		}
	}

	public void addSignPostCrumb(String label, String ref) {
		Crumb crumb = (Crumb) signPostMap.get(ref);
		Crumb signPost = new Crumb(ref);
		crumb.getSubMenu().addItem(signPost);
	}

	public Map getSignPosts() {
		return signPostMap;
	}

	public void clearSignPost(Crumb signPost) {
		signPost.getSubMenu().clearItems();
	}

	public void clearAllSignPosts() {
		for (Iterator iter = signPostMap.keySet().iterator(); iter.hasNext();) {
			String ref = (String) iter.next();
			Crumb element = (Crumb) signPostMap.get(ref);
			clearSignPost(element);
		}
		signPostMap.clear();
	}
	
	public void clearAll() {
		clearAllSignPosts();
		bar.clearItems();
	}

	public void execute(Crumb itm) {
		if (listener != null)
			listener.onSelect(itm);
	}

	protected void refreshDynamicItem(Crumb itm) {
		if (listener != null)
			listener.onLoadSignPostCrumbs(itm);
	}

	protected class Bar extends MenuBar {
		public Bar(boolean vert) {
			super(vert);
			setStyleName("gems-BreadcrumbBar");
		}

		public void onBrowserEvent(Event event) {
			isInBrowserEvent = true;
			super.onBrowserEvent(event);
			isInBrowserEvent = false;
		}
	}

	public class Crumb extends MenuItem implements Command {
		public String reference;

		public Bar signPostMenu;

		public Crumb(String ref, String text, boolean asHTML) {
			super(text, asHTML, (Command) null);
			reference = ref;
			setStyleName("gems-Crumb");
			setCommand(this);
		}

		public Crumb(String ref) {
			super("&gt;", true, (MenuBar) null);
			reference = ref;
			setStyleName("gems-SignPost");
			signPostMenu = new Bar(true);
			setSubMenu(signPostMenu);
		}

		public MenuBar getSubMenu() {
			if (isInBrowserEvent && !doneDynamicRefresh) {
				refreshDynamicItem(this);
				doneDynamicRefresh = true;
			}
			return super.getSubMenu();
		}

		public void execute() {
			BreadcrumbBar.this.execute(this);
		}
	}
}
